﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace The_LarX.Rendering
{
    public class Render2Texture
    {
        public RenderTarget2D RenderTarget;

        public Color BackgroundColor = Color.Transparent;
        public bool ClearBackground = true;

        public Render2Texture()
            : this(Engine.GraphicsDevice.PresentationParameters.BackBufferWidth,
            Engine.GraphicsDevice.PresentationParameters.BackBufferHeight)
        {
        }

        /// <summary>
        /// Initializes a Render2Textures Instance. The RenderTarget properties will
        /// be set to the Default from the GraphicsDevice's Presentation Paramenters
        /// </summary>
        /// <param name="TexWidth"></param>
        /// <param name="TexHeight"></param>
        public Render2Texture(int TexWidth, int TexHeight)
        {
            PresentationParameters pp = Engine.GraphicsDevice.PresentationParameters;
            RenderTarget = new RenderTarget2D(Engine.GraphicsDevice,
                                             TexWidth, TexHeight, false,
                                             pp.BackBufferFormat, pp.DepthStencilFormat);
        }

        /// <summary>
        /// Initializes a Render2Textures Instance. The RenderTarget properties will
        /// be set to the Default from the GraphicsDevice's Presentation Paramenters
        /// </summary>
        /// <param name="TexWidth"></param>
        /// <param name="TexHeight"></param>
        public Render2Texture(int TexWidth, int TexHeight, RenderTargetUsage usage)
        {
            PresentationParameters pp = Engine.GraphicsDevice.PresentationParameters;
            RenderTarget = new RenderTarget2D(Engine.GraphicsDevice,
                                             TexWidth, TexHeight, false,
                                             pp.BackBufferFormat, pp.DepthStencilFormat, pp.MultiSampleCount,
                                             usage);
        }

        public bool LastWasBegin;

        public void BeginRender()
        {
            if (LastWasBegin)
            {
                LarExceptionHandler.AddException(new Exception("You need to call EndRender() before calling"
                    + "BeginRender() again!"));
            }
            
            if (RenderTarget.IsDisposed)
            {
                Disable();
                return;
            }

            Engine.GraphicsDevice.SetRenderTarget(RenderTarget);

            if (ClearBackground)
            {
                Engine.GraphicsDevice.Clear(BackgroundColor);
            }
            LastWasBegin = true;
        }
        public void EndRender()
        {
            if (!LastWasBegin)
            {
                LarExceptionHandler.AddException(new Exception("You need to call BeginRender() before calling"
                    + "EndRender() again!"));
            }
            
            Engine.GraphicsDevice.SetRenderTarget(null);

            if (ClearBackground)
            {
                Engine.RenderManager.Clear();
            }

            LastWasBegin = false;
        }

        public void Disable()
        {
            if (RenderTarget != null && !RenderTarget.IsDisposed)
            {
                RenderTarget.Dispose();
            }
        }
    }
}
